import * as Cesium from "cesium";

// 定义材质的默认参数
const defaultColor = Cesium.Color.fromCssColorString("#FF0000");
const defaultSpeed = 1.0;
const defaultCount = 3;
const defaultGradient = 0.1;

// 扩展 Material 类型
declare module "cesium" {
  interface Material {
    update?: (scene: Cesium.Scene) => void;
  }
  namespace Material {
    let _materialCache: {
      addMaterial: (name: string, config: any) => void;
    };
  }
  interface Scene {
    frameState: {
      time: number;
    };
  }
}

// 定义材质的 GLSL 代码
const DynamicCircleDiffuseMaterialSource = `
    uniform vec4 color;
    uniform float speed;
    uniform float count;
    uniform float gradient;
    uniform float time;

    czm_material czm_getMaterial(czm_materialInput materialInput) {
        czm_material material = czm_getDefaultMaterial(materialInput);

        // 计算 UV 坐标
        vec2 st = materialInput.st;
        vec2 center = vec2(0.5, 0.5);
        float distance = length(st - center) * 2.0;

        // 计算动态波纹
        float progress = fract(time * speed);
        float wave = 0.0;

        // 创建多个圆环
        for(float i = 0.0; i < 10.0; i++) {
            if(i >= count) break;
            float waveProgress = fract(progress + i / count);
            float waveDistance = abs(distance - waveProgress);
            wave += 1.0 - smoothstep(0.0, gradient, waveDistance);
        }

        // 设置材质颜色和透明度
        material.diffuse = color.rgb;
        material.alpha = wave * color.a;

        return material;
    }
`;

// 创建材质类
class DynamicCircleDiffuseMaterial {
  private static MaterialType = "DynamicCircleDiffuse";
  private static isInitialized = false;

  /**
   * 初始化材质
   */
  private static initialize() {
    if (this.isInitialized) {
      return;
    }

    Cesium.Material._materialCache.addMaterial(this.MaterialType, {
      fabric: {
        type: this.MaterialType,
        uniforms: {
          color: defaultColor,
          speed: defaultSpeed,
          count: defaultCount,
          gradient: defaultGradient,
          time: 0,
        },
        source: DynamicCircleDiffuseMaterialSource,
      },
      translucent: () => true,
    });

    this.isInitialized = true;
  }

  /**
   * 创建材质实例
   * @param options 材质参数
   * @returns Cesium.Material 实例
   */
  static create(
    options: {
      color?: Cesium.Color;
      speed?: number;
      count?: number;
      gradient?: number;
    } = {}
  ) {
    this.initialize();

    const material = new Cesium.Material({
      fabric: {
        type: this.MaterialType,
        uniforms: {
          color: options.color || defaultColor,
          speed: options.speed || defaultSpeed,
          count: options.count || defaultCount,
          gradient: options.gradient || defaultGradient,
          time: 0,
        },
      },
    });

    // 添加时间更新
    material.update = function (scene: Cesium.Scene) {
      this.uniforms.time = scene.frameState.time / 1000.0;
    };

    return material;
  }
}

export default DynamicCircleDiffuseMaterial;
